//
//  BSNavigationController.m
//  BaiSi
//
//  Created by Sycamore on 16/4/27.
//  Copyright © 2016年 Sycamore. All rights reserved.
//

#import "BSNavigationController.h"

@interface BSNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation BSNavigationController

+(void)load
{
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self , nil];
    [bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    //去掉阴影线
    bar.shadowImage = [[UIImage alloc]init];
    
    
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:21];
    attr[NSForegroundColorAttributeName] = [UIColor blackColor];
    [bar setTitleTextAttributes:attr];
    
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:18];
    attrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedIn:self, nil];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    
    //系统自己的侧滑手势，UILayoutCntainerView就是导航控制器或者tabBarController自身的view
//    <UIScreenEdgePanGestureRecognizer: 0x7ff7f8f2c880;
//    state = Possible; delaysTouchesBegan = YES;
//    view = <UILayoutContainerView 0x7ff7f8c86660>;
//    target= <(action=handleNavigationTransition:,
//    target=<_UINavigationInteractiveTransition 0x7ff7f8f2a6a0>)>>
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    [self.view addGestureRecognizer:pan];
    pan.delegate = self;
    
    
    //是否接受侧滑手势由自己管理，不用系统的私有类来管理
//    self.interactivePopGestureRecognizer.delegate = self;
    
    //当实现了全屏侧滑的时候，系统的自带的侧滑就不要使用了，以免造成冲突
    self.interactivePopGestureRecognizer.enabled = NO;

}


//拦截push
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count > 0) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"返回" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        
        [button setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [button sizeToFit];
        
        //设置内容偏移前一定要设置好尺寸，不然没有效果的
        button.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
        
        //隐藏底部tabBar必须在push前设置才会有效的
        viewController.hidesBottomBarWhenPushed = YES;
        
    }
    [super pushViewController:viewController animated:YES];
}


- (void)back
{
    [self popViewControllerAnimated:YES];
}


#pragma mark UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return self.childViewControllers.count > 1;
}
@end
